Skip to content

Optimize pathfinding with A* and enhance Directions UI - #237

Open
nschimme wants to merge 6 commits into
masterfrom
optimize-dirs-command-a-star-14194329936225864437
Open

Optimize pathfinding with A* and enhance Directions UI#237
nschimme wants to merge 6 commits into
masterfrom
optimize-dirs-command-a-star-14194329936225864437

Conversation

@nschimme

@nschimme nschimme commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Investigated and optimized pathfinding in MMapper.

Key improvements:

  • A Algorithm:* Added MapData::shortestPathSearchPointToPoint implementing A* search. This provides a performance boost when a specific destination is known.
  • Improved UX:
    • The "Find Rooms" dialog now shows the Euclidean distance to matching rooms.
    • Added a "Directions" button to the "Find Rooms" dialog to quickly see the path to a room.
    • Added a "Directions" option to the map's right-click context menu.
  • Architectural Cleanup: Refined the communication between UI components and the pathfinding engine, ensuring thread safety and proper access to the active session's parser.
  • Stability: Fixed build regressions and handled edge cases where the player's position might be unknown.

PR created automatically by Jules for task 14194329936225864437 started by @nschimme

Summary by Sourcery

Introduce point-to-point A* pathfinding and expose direct directions workflows from search results and map context menus.

New Features:

  • Add MapData::shortestPathSearchPointToPoint implementing A* search between two rooms.
  • Allow users to request directions to specific rooms directly from the Find Rooms dialog and map context menu.
  • Display player-relative distance for each room result in the Find Rooms dialog.

Bug Fixes:

  • Guard against missing proxies or unknown player positions when resolving directions or distance calculations.

Enhancements:

  • Wire UI components to use the active session's parser when requesting directions, ensuring proper session context and safety.
  • Extend the Find Rooms results table with an additional distance column and associated tooltips.

- Implemented point-to-point A* search in MapData using an admissible
  Euclidean distance heuristic.
- Enhanced "Find Rooms" dialog with a "Distance" column and a
  "Directions" button.
- Added a "Directions" entry to the map context menu when rooms are
  selected.
- Exposed the active parser to MainWindow via ConnectionListener to
  facilitate UI-triggered pathfinding.
- Maintained Dijkstra for multi-target searches where it is more efficient.
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces an A*-based point-to-point pathfinding routine and wires it into parser and UI flows (Find Rooms dialog and map context menu) so users can request directions to specific rooms, while also surfacing approximate Euclidean distance to search results and tightening parser access via ConnectionListener.

Sequence diagram for the new directions-to-room flow

sequenceDiagram
    actor User
    participant FindRoomsDlg
    participant MainWindow
    participant ConnectionListener
    participant AbstractParser
    participant MapData
    participant ShortestPathRecipient

    User->>FindRoomsDlg: click_directionsButton
    FindRoomsDlg-->>MainWindow: sig_getDirections(RoomId)

    MainWindow->>ConnectionListener: getUserParser()
    ConnectionListener-->>MainWindow: AbstractParser*

    MainWindow->>AbstractParser: doGetDirectionsToRoom(RoomHandle)
    AbstractParser->>MapData: shortestPathSearchPointToPoint(origin, target, recipient)
    MapData-->>ShortestPathRecipient: receiveShortestPath(sp_nodes, targetIndex)
Loading

File-Level Changes

Change Details Files
Add A*-based point-to-point shortest-path search and associated heuristic helper.
  • Implement euclidean_distance helper that weights vertical distance more heavily.
  • Add MapData::shortestPathSearchPointToPoint using A* with a min-heap style priority_queue and g-score map.
  • Handle origin==target as a trivial path with a single SPNode.
  • Preserve existing Dijkstra-based shortestPathSearch for multi-target use cases while documenting tradeoffs.
src/mapdata/shortestpath.cpp
src/mapdata/mapdata.h
Expose a parser entrypoint for computing directions to an explicit RoomHandle and surface parser access via ConnectionListener.
  • Add AbstractParser::doGetDirectionsToRoom that uses MapData::shortestPathSearchPointToPoint from the player’s tail position to a target room.
  • Add ConnectionListener::getUserParser to safely retrieve the user parser instance if available.
  • Declare AbstractParser forward in connectionlistener.h and make Proxy a friend of ConnectionListener so it can access getUserParser implementation details.
src/parser/abstractparser.cpp
src/parser/abstractparser.h
src/proxy/connectionlistener.cpp
src/proxy/connectionlistener.h
src/proxy/proxy.h
Wire new directions flow into MainWindow UI, enabling directions from room selections and Find Rooms dialog.
  • Create a Directions QAction, add it to the selected-room action group, and insert it into the map context menu above edit/move/delete actions.
  • Implement MainWindow::slot_onDirections to sanitize the room selection and request directions for each selected room via the parser if available.
  • Connect FindRoomsDlg::sig_getDirections to a lambda in MainWindow that resolves the RoomId and forwards to the parser.
  • Register the new directions action and slot in mainwindow.h.
src/mainwindow/mainwindow.cpp
src/mainwindow/mainwindow.h
Enhance the Find Rooms dialog to compute and display distance to each result and to emit a directions request for selected rows.
  • Add a Distance column to the results table and update headers/tooltips accordingly.
  • Compute Euclidean distance from the player’s current position (if known) to each found room; show '-' when position is unavailable.
  • Enable/disable the Directions button in sync with selection state and reset it on dialog close.
  • On Directions button click, resolve selected rows to room IDs and emit sig_getDirections for each.
src/mainwindow/findroomsdlg.cpp
src/mainwindow/findroomsdlg.h
src/mainwindow/findroomsdlg.ui

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The distance shown in the FindRooms dialog uses an unweighted Euclidean distance, while the A* heuristic (euclidean_distance) treats Z as more expensive; consider reusing the same helper or at least the same weighting to keep UX-consistent and avoid confusing discrepancies between “closest” and “shortest path.”
  • The logic to resolve a RoomHandle and call doGetDirectionsToRoom is duplicated in the FindRooms dialog signal handler and MainWindow::slot_onDirections; consider factoring this into a small helper in MainWindow to avoid divergence in behavior over time (e.g., future null checks or preconditions).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The distance shown in the FindRooms dialog uses an unweighted Euclidean distance, while the A* heuristic (euclidean_distance) treats Z as more expensive; consider reusing the same helper or at least the same weighting to keep UX-consistent and avoid confusing discrepancies between “closest” and “shortest path.”
- The logic to resolve a RoomHandle and call doGetDirectionsToRoom is duplicated in the FindRooms dialog signal handler and MainWindow::slot_onDirections; consider factoring this into a small helper in MainWindow to avoid divergence in behavior over time (e.g., future null checks or preconditions).

## Individual Comments

### Comment 1
<location path="src/parser/abstractparser.cpp" line_range="505-508" />
<code_context>
     }
 }

+void AbstractParser::doGetDirectionsToRoom(const RoomHandle &target)
+{
+    ShortestPathEmitter sp_emitter(*this);
+    if (const auto r = m_mapData.findRoomHandle(getTailPosition())) {
+        MapData::shortestPathSearchPointToPoint(r, target, sp_emitter);
+    }
</code_context>
<issue_to_address>
**suggestion:** Handle missing origin room more explicitly when computing directions

If `getTailPosition()` can reference a non-existent room, this function will silently do nothing. Consider either falling back to another position (e.g., the current player location) or at least logging/emitting feedback so callers know why no directions were produced.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/parser/abstractparser.cpp Outdated
Comment on lines +505 to +508
void AbstractParser::doGetDirectionsToRoom(const RoomHandle &target)
{
ShortestPathEmitter sp_emitter(*this);
if (const auto r = m_mapData.findRoomHandle(getTailPosition())) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Handle missing origin room more explicitly when computing directions

If getTailPosition() can reference a non-existent room, this function will silently do nothing. Consider either falling back to another position (e.g., the current player location) or at least logging/emitting feedback so callers know why no directions were produced.

nschimme added 2 commits June 10, 2026 00:25
- Implemented point-to-point and multi-target A* search in MapData.
- Used admissible Manhattan distance heuristic scaled by minimum edge
  cost (0.65) to ensure consistency with original Dijkstra search.
- Integrated A* into the `_dirs` command for faster pattern-based searches.
- Enhanced "Find Rooms" dialog with a "Distance" column and a
  "Directions" button.
- Added a "Directions" entry to the map context menu.
- Exposed the active parser to MainWindow via ConnectionListener to
  facilitate UI-triggered pathfinding.
- Strictly maintained original movement cost logic (terrain, doors,
  ridability, etc.) for all pathfinding.
- Implemented point-to-point and multi-target A* search in MapData.
- Used admissible Manhattan distance heuristic scaled by minimum edge
  cost (0.65) to ensure consistency with original Dijkstra search.
- Integrated A* into the `_dirs` command for faster pattern-based searches.
- Enhanced "Find Rooms" dialog with a "Distance" column and a
  "Directions" button.
- Added a "Directions" entry to the map context menu.
- Exposed the active parser to MainWindow via ConnectionListener to
  facilitate UI-triggered pathfinding.
- Strictly maintained original movement cost logic (terrain, doors,
  ridability, etc.) for all pathfinding.
- Fixed formatting and compilation issues.
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 353 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (c8baaf4) to head (8a661e8).

Files with missing lines Patch % Lines
src/map-utils/CommandNames.cpp 0.00% 157 Missing ⚠️
src/mapdata/shortestpath.cpp 0.00% 109 Missing ⚠️
src/mainwindow/UpdateDialog.cpp 0.00% 40 Missing ⚠️
src/global/CompareVersion.cpp 0.00% 21 Missing ⚠️
src/mainwindow/findroomsdlg.cpp 0.00% 21 Missing ⚠️
tests/TestMap.cpp 0.00% 4 Missing ⚠️
src/global/CompareVersion.h 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #237       +/-   ##
==========================================
- Coverage   25.69%   0.00%   -25.70%     
==========================================
  Files         521     365      -156     
  Lines       43187   27837    -15350     
  Branches     4708    3185     -1523     
==========================================
- Hits        11099       0    -11099     
+ Misses      32088   27837     -4251     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

nschimme added 3 commits June 10, 2026 12:21
- Implemented point-to-point and multi-target A* search in MapData.
- Used admissible Manhattan distance heuristic scaled by minimum edge
  cost (0.65) to ensure consistency with original Dijkstra search.
- Integrated A* into the `_dirs` command for faster pattern-based searches.
- Enhanced "Find Rooms" dialog with a "Distance" column and a
  "Directions" button.
- Added a "Directions" entry to the map context menu.
- Exposed the active parser to MainWindow via ConnectionListener to
  facilitate UI-triggered pathfinding.
- Strictly maintained original movement cost logic (terrain, doors,
  ridability, etc.) for all pathfinding.
- Fixed 64-bit to 32-bit integer conversion warnings.
- Implemented point-to-point and multi-target A* search in MapData.
- Used admissible Manhattan distance heuristic scaled by minimum edge
  cost (0.65) to ensure consistency with original Dijkstra search.
- Integrated A* into the `_dirs` command for faster pattern-based searches.
- Enhanced "Find Rooms" dialog with a "Distance" column and a
  "Directions" button.
- Added a "Directions" entry to the map context menu.
- Exposed the active parser to MainWindow via ConnectionListener to
  facilitate UI-triggered pathfinding.
- Strictly maintained original movement cost logic (terrain, doors,
  ridability, etc.) for all pathfinding.
- Fixed 64-bit to 32-bit integer conversion warnings.
- Moved `Abbrev` and `CompareVersion` to `src/global` to resolve
  cross-library dependency issues in tests.
- Created `src/map-utils/CommandNames` to house command mapping logic,
  allowing `mm_map` to remain independent of the parser.
- Updated `src/CMakeLists.txt` to correctly partition map-related
  subdirectories and link `mm_map` with `Qt6::Network` and `Qt6::OpenGL`.
- Implemented A* search algorithm for point-to-point and multi-target
  pathfinding in `MapData`, maintaining consistency with movement costs.
- Enhanced "Find Rooms" dialog with distances and a "Directions" button.
- Added "Directions" action to the map context menu.
- Fixed 64-bit integer conversion warnings and ensured optimal paths.
- Verified all unit tests (`TestMap`, `TestExpandoraCommon`,
  `TestMainWindow`) pass with the new library structure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant